<!DOCTYPE html>
<html lang="en">
<head>
        <meta charset="utf-8" />
        <title>T-appendix.jupyter-installation-and-setup</title>
        <link rel="stylesheet" href="/theme/css/main.css" />

        <!--[if IE]>
            <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
</head>

<body id="index" class="home">
        <header id="banner" class="body">
                <h1><a href="/">PythonTutorials </a></h1>
                <nav><ul>
                    <li><a href="/category/cpp.html">cpp</a></li>
                    <li class="active"><a href="/category/python.html">python</a></li>
                </ul>
                </nav>
        </header><!-- /#banner -->
<section id="content" class="body">
  <article>
    <header>
      <h1 class="entry-title">
        <a href="/T-appendix.jupyter-installation-and-setup.html" rel="bookmark"
           title="Permalink to T-appendix.jupyter-installation-and-setup">T-appendix.jupyter-installation-and-setup</a></h1>
    </header>

    <div class="entry-content">
<footer class="post-info">
        <span>Mon 06 April 2020</span>
<span>| tags: <a href="/tag/python.html">python</a><a href="/tag/progarmming.html">progarmming</a><a href="/tag/excel.html">Excel</a></span>
</footer><!-- /.post-info -->      <h1>Jupyterlab 的安装与配置</h1>
<h2>下载并安装 Anaconda</h2>
<p><a href="https://www.anaconda.com">Anaconda</a> 是目前最方便的 Python 发行版，搭载了很多我们终将必用的软件包，除了 Python 之外，还有 <a href="https://www.r-project.org/">R 语言</a>，还包括 <a href="https://pandoc.org/">Pandoc</a>，<a href="http://www.numpy.org/">NumPy</a>，<a href="https://www.scipy.org/">SciPy</a>，<a href="https://matplotlib.org/">Matplotlib</a>…… 等等。</p>
<p>无论是图形化界面安装，还是命令行界面安装，建议都把 Anaconda 安装在本地用户目录内，<code>~/</code>。请下载并安装 Python 3.x 的版本。</p>
<p>图形化界面安装的教程，官方的很详细，各个操作平台的说明都有：</p>
<blockquote>
<p>https://docs.anaconda.com/anaconda/install/</p>
</blockquote>
<p>在 MacOS 的 Terminal 命令行下，可以直接下载并安装：</p>
<div class="highlight"><pre><span></span><span class="nb">cd</span> ~/Downloads/
wget https://repo.anaconda.com/archive/Anaconda3-2018.12-MacOSX-x86_64.sh
chmod +x Anaconda3-2018.12-MacOSX-x86_64.sh
./Anaconda3-2018.12-MacOSX-x86_64.sh
</pre></div>


<p>安装到最后一步，会问你是否要安装微软出品的 <a href="https://code.visualstudio.com">Visual Studio Code</a>，选择 <code>yes</code> —— 反正以后你的电脑上会不止一个文本编辑器…… 以后你可能还会安装的文本编辑器包括 <a href="https://www.sublimetext.com">SublimeText</a>, <a href="https://atom.io">Atom</a> 等等。</p>
<p>安装完毕之后，打开 Terminal(Windows 系统需要打开之前安装的 Anaconda Prompt 输入），继续安装几个组件：</p>
<div class="highlight"><pre><span></span>conda update conda
conda update anaconda
conda install -c conda-forge nodejs
conda install -c conda-forge jupyterlab <span class="c1"># 这是用来升级 jupyter lab 到最新版的方法</span>
</pre></div>


<p>安装完毕之后，可以看看各个你将要用到的可执行命令都在什么地方，用 <code>which</code> 命令（windows下用 <code>where</code> 命令）：</p>
<div class="highlight"><pre><span></span>which python
python --version
which node
node -v
which jupyter
jupyter lab --version
jupyter notebook --version
which pip
pip --version
</pre></div>


<h2>第一次启动 Jupyter lab</h2>
<p>打开 Terminal，<code>cd</code> 到你想打开 Jupyter lab 的目录（就是你保存 <code>ipynb</code> 文件的地方，以便在 Jupyter lab 中打开、浏览、编辑 <code>ipynb</code> 文件），在这里以用户根目录为例 <code>~/</code>：</p>
<div class="highlight"><pre><span></span><span class="nb">cd</span> ~
jupyter lab
</pre></div>


<p>此时的 Terminal 窗口不能关闭，否则 Jupyter lab 就停止运行了 —— 就将它放在那里。</p>
<p>随后会有个浏览器打开，指向 <a href="http://localhost:8888/lab?">http://localhost:8888/lab?</a> —— 你就看到 Jupyter lab 的操作界面了。</p>
<p>目前，Jupyter lab 和 Jupyter notebook 是并存的，虽然前者是后者的下一步替代者。如果你依然习惯于使用 Jupyter notebook，那么，在浏览器中指向 <a href="http://localhost:8888/tree?">http://localhost:8888/tree?</a> 看到的就是 Jupyter notebook.</p>
<h2>配置 Jupyter lab</h2>
<p>打开 Terminal，输入以下命令：</p>
<div class="highlight"><pre><span></span>jupyter lab --generate-config
</pre></div>


<p>这会在 <code>~/.jupyter/</code> 目录下生成一个 <code>jupyter_notebook_config.py</code> 文件。</p>
<div class="highlight"><pre><span></span><span class="nb">cd</span> ~/.jupyter
code jupyter_notebook_config.py
</pre></div>


<p>上面的 code 命令，需要你已经安装 Visual Studio Code，并且在已经在其中设置了 <code>Install 'code' command in PATH</code>。参见附录 <a href="T-appendix.editor.vscode.md">Visual Studio Code 的安装与配置</a></p>
<p>事实上，你可以用你喜欢的任何编辑器打开 <code>~/.jupyter/jupyter_notebook_config.py</code> 文件。</p>
<p>文件内容很长，有空可以仔细看。可以直接将以下内容拷贝粘贴到文件底部，根据需求修改：</p>
<div class="highlight"><pre><span></span><span class="err">#c.NotebookApp.token</span> <span class="err">=</span> <span class="err">&#39;&#39;</span>
<span class="err">#c.NotebookApp.open_browser</span> <span class="err">=</span> <span class="err">False</span>
<span class="err">#c.NotebookApp.notebook_dir</span> <span class="err">=</span> <span class="err">&#39;~/&#39;</span>
<span class="err">#c.NotebookApp.default_url</span> <span class="err">=</span> <span class="err">&#39;/tree&#39;</span>
</pre></div>


<p>逐条解释一下：</p>
<blockquote>
<p><code>c.NotebookApp.token = ''</code></p>
</blockquote>
<p>每次打开 jupyter，它都会给你生成一个新的 Token —— 这是安全策略。但是，如果你只是在自己的电脑上使用，那么，这就给你制造了麻烦，因为若是你想同时用另外一个浏览器打开它，那你就需要从 Terminal 里拷贝那个 Token 出来。所以，你可以在配置文件里直接把它设置为空。</p>
<blockquote>
<p><code>c.NotebookApp.open_browser = False</code></p>
</blockquote>
<p>每次你执行 <code>jupyter lab</code> 或者 <code>jupyter notebook</code> 命令的时候，它都会使用系统默认浏览器。</p>
<p>每个人的习惯不一样。比如我，会想到用一个平时不怎么用的浏览器专门用在 Jupyter 上，这样会防止自己在关闭其它网页的时候不小心把 Jupyter 关掉…… 那我就会把这项设定为 <code>False</code>。</p>
<blockquote>
<p><code>c.NotebookApp.notebook_dir = '~/'</code></p>
</blockquote>
<p>在 Terminal 中执行 <code>jupyter</code> 命令的时候，它默认是在你当前所在的工作目录打开 <code>jupyter</code>，这同样是出于安全考虑。但是，如果你只是在自己的电脑上使用，且只有自己在使用，那么莫不如直接把它设置成 <code>~/</code>，即，你的用户根目录，这样会很方便地访问各种地方的文件……</p>
<blockquote>
<p><code>c.NotebookApp.default_url = '/tree'</code></p>
</blockquote>
<p>这一项留给那些依然习惯于使用 jupyter notebook 的人，这样设置之后，即便是输入 <code>jupyter lab</code> 命令，打开的还是 jupyter notebook。</p>
<p>在 Terminal 里常用的与 Jupyter 有关的命令有：</p>
<div class="highlight"><pre><span></span>jupyter lab
jupyter lab --version
conda install -c conda-forge jupyterlab <span class="c1"># 这是用来升级 jupyter lab 到最新版的方法</span>
jupyter notebook list                   <span class="c1"># 查看正在运行的 jupyter lab/notebook</span>
jupyter notebook stop                   <span class="c1"># 停止 jupyter lab/notebook 服务</span>
</pre></div>


<h2>将 Jupyter lab 配置成系统服务</h2>
<p>如果，你厌烦每次都要跑到 Terminal 里启动 Jupyter lab，可以把它配置成系统服务，每次开机启动它就自动运行。而你需要做的只不过是直接从浏览器中访问 <a href="http://localhost:8888/">http://localhost:8888/</a>。</p>
<div class="highlight"><pre><span></span>code ~/Library/LaunchAgents/com.jupyter.lab.plist
</pre></div>


<p>这条命令会让 Visual Studio Code 创建 <code>~/Library/LaunchAgents/com.jupyter.lab.plist</code> 文件并打开。</p>
<p>在其中拷贝粘贴以下内容，注意，要把其中的 <code>your_username</code> 修改为你的用户名：</p>
<div class="highlight"><pre><span></span><span class="cp">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span>
<span class="cp">&lt;!DOCTYPE plist PUBLIC &quot;-//Apple//DTD PLIST 1.0//EN&quot; &quot;http://www.apple.com/DTDs/PropertyList-1.0.dtd&quot;&gt;</span>
<span class="nt">&lt;plist</span> <span class="na">version=</span><span class="s">&quot;1.0&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;dict&gt;</span>
    <span class="nt">&lt;key&gt;</span>Label<span class="nt">&lt;/key&gt;</span>
    <span class="nt">&lt;string&gt;</span>local.job<span class="nt">&lt;/string&gt;</span>
    <span class="nt">&lt;key&gt;</span>ProgramArguments<span class="nt">&lt;/key&gt;</span>
    <span class="nt">&lt;array&gt;</span>
        <span class="nt">&lt;string&gt;</span>/Users/your_username/anaconda3/bin/jupyter<span class="nt">&lt;/string&gt;</span>
        <span class="nt">&lt;string&gt;</span>lab<span class="nt">&lt;/string&gt;</span>
        <span class="nt">&lt;string&gt;</span>--no-browser<span class="nt">&lt;/string&gt;</span>
        <span class="nt">&lt;string&gt;</span>--notebook-dir=/Users/your_username/<span class="nt">&lt;/string&gt;</span>
    <span class="nt">&lt;/array&gt;</span>
    <span class="nt">&lt;key&gt;</span>RunAtLoad<span class="nt">&lt;/key&gt;</span>
    <span class="nt">&lt;true/&gt;</span>
    <span class="nt">&lt;key&gt;</span>StandardErrorPath<span class="nt">&lt;/key&gt;</span>
    <span class="nt">&lt;string&gt;</span>/tmp/local.job.err<span class="nt">&lt;/string&gt;</span>
    <span class="nt">&lt;key&gt;</span>StandardOutPath<span class="nt">&lt;/key&gt;</span>
    <span class="nt">&lt;string&gt;</span>/tmp/local.job.out<span class="nt">&lt;/string&gt;</span>
<span class="nt">&lt;/dict&gt;</span>
<span class="nt">&lt;/plist&gt;</span>
</pre></div>


<p>如果之前在 <code>jupyter_notebook_config.py</code> 文件里已经设置过</p>
<div class="highlight"><pre><span></span><span class="err">c.NotebookApp.open_browser</span> <span class="err">=</span> <span class="err">False</span>
<span class="err">c.NotebookApp.notebook_dir</span> <span class="err">=</span> <span class="err">&#39;~/&#39;</span>
</pre></div>


<p>那么这两行就可以不要了：</p>
<div class="highlight"><pre><span></span>        <span class="nt">&lt;string&gt;</span>--no-browser<span class="nt">&lt;/string&gt;</span>
        <span class="nt">&lt;string&gt;</span>--notebook-dir=/Users/your_username/<span class="nt">&lt;/string&gt;</span>
</pre></div>


<p>而后在 Terminal 里执行：</p>
<div class="highlight"><pre><span></span>launchctl load ~/Library/LaunchAgents/com.jupyter.lab.plist
</pre></div>


<p>如果你想重新启动这个服务，那么执行：</p>
<div class="highlight"><pre><span></span>launchctl unload ~/Library/LaunchAgents/com.jupyter.lab.plist
launchctl load ~/Library/LaunchAgents/com.jupyter.lab.plist
</pre></div>


<h2>关于 Jupyter lab themes</h2>
<p>对中文用户来说，Jupyter 的默认字号有点过小，阅读起来不是很舒适。但最佳的方案不是去寻找合适的 themes，而是直接使用支持 <a href="https://github.com/openstyles/stylus">Stylus</a> 这类终端 CSS 定制插件的浏览器，Chrome/Firefox/Opera 都支持 Stylus 插件。</p>
<p>我用的 Stylus 定制 CSS 是这样的：</p>
<div class="highlight"><pre><span></span><span class="nt">a</span> <span class="p">{</span><span class="k">color</span><span class="p">:</span> <span class="mh">#2456A4</span> <span class="cp">!important</span><span class="p">;}</span>
<span class="nt">strong</span> <span class="p">{</span><span class="k">color</span><span class="p">:</span><span class="mh">#6392BF</span><span class="p">;}</span>
<span class="nt">em</span> <span class="p">{</span><span class="k">color</span><span class="p">:</span> <span class="mh">#A9312A</span><span class="p">;</span> <span class="k">font-style</span><span class="p">:</span> <span class="kc">normal</span> <span class="cp">!important</span><span class="p">;}</span>
<span class="nt">table</span> <span class="p">{</span><span class="k">font-size</span><span class="p">:</span> <span class="mi">90</span><span class="kt">%</span> <span class="cp">!important</span><span class="p">;}</span>

<span class="p">#</span><span class="nn">jp-main-dock-panel</span> <span class="p">{</span><span class="k">background-color</span><span class="p">:</span> <span class="mh">#f9f9f9</span><span class="p">;}</span>
<span class="p">.</span><span class="nc">jp-RenderedHTMLCommon</span> <span class="p">{</span><span class="k">font-family</span><span class="p">:</span> <span class="s2">&quot;Yuanti SC&quot;</span><span class="p">;</span> <span class="k">font-size</span><span class="p">:</span> <span class="mi">100</span><span class="kt">%</span><span class="p">;}</span>
<span class="p">.</span><span class="nc">jp-Notebook</span> <span class="p">{</span><span class="k">background-color</span><span class="p">:</span> <span class="mh">#fbfafa</span><span class="p">;}</span>
<span class="p">.</span><span class="nc">CodeMirror</span><span class="o">,</span> <span class="p">.</span><span class="nc">jp-RenderedHTMLCommon</span> <span class="nt">pre</span> <span class="p">{</span><span class="k">font-size</span><span class="p">:</span> <span class="mi">90</span><span class="kt">%</span><span class="p">;}</span>
<span class="p">.</span><span class="nc">jp-RenderedHTMLCommon</span> <span class="nt">pre</span> <span class="p">{</span>
    <span class="k">padding</span><span class="p">:</span> <span class="mi">10</span><span class="kt">px</span> <span class="mi">25</span><span class="kt">px</span><span class="p">;</span>
    <span class="k">background-color</span><span class="p">:</span> <span class="mh">#fafafa</span><span class="p">;</span>
    <span class="k">border-left</span><span class="p">:</span> <span class="mi">4</span><span class="kt">px</span> <span class="kc">solid</span> <span class="mh">#dadada</span><span class="p">;</span>
    <span class="k">border-radius</span><span class="p">:</span> <span class="mi">10</span><span class="kt">px</span><span class="p">;</span>
<span class="p">}</span>

<span class="p">.</span><span class="nc">jp-RenderedHTMLCommon</span> <span class="nt">pre</span> <span class="nt">code</span> <span class="p">{</span>
    <span class="k">background-color</span><span class="p">:</span> <span class="mh">#fafafa</span><span class="p">;</span>
<span class="p">}</span>

<span class="p">.</span><span class="nc">jp-RenderedHTMLCommon</span> <span class="nt">h1</span> <span class="nt">code</span><span class="o">,</span>
<span class="p">.</span><span class="nc">jp-RenderedHTMLCommon</span> <span class="nt">h2</span> <span class="nt">code</span><span class="o">,</span>
<span class="p">.</span><span class="nc">jp-RenderedHTMLCommon</span> <span class="nt">h3</span> <span class="nt">code</span><span class="o">,</span>
<span class="p">.</span><span class="nc">jp-RenderedHTMLCommon</span> <span class="nt">h4</span> <span class="nt">code</span><span class="o">,</span>
<span class="p">.</span><span class="nc">jp-RenderedHTMLCommon</span> <span class="nt">p</span> <span class="nt">code</span><span class="o">,</span>
<span class="p">.</span><span class="nc">jp-RenderedHTMLCommon</span> <span class="nt">li</span> <span class="nt">code</span><span class="o">,</span>
<span class="p">.</span><span class="nc">jp-RenderedHTMLCommon</span> <span class="nt">blockquote</span> <span class="nt">p</span> <span class="nt">code</span><span class="o">,</span>
<span class="p">.</span><span class="nc">jp-RenderedHTMLCommon</span> <span class="nt">blockquote</span> <span class="nt">li</span> <span class="nt">code</span><span class="o">,</span>
<span class="p">.</span><span class="nc">jp-RenderedHTMLCommon</span> <span class="nt">td</span> <span class="nt">code</span> <span class="p">{</span>
    <span class="k">background-color</span><span class="p">:</span> <span class="mh">#f6f6f6</span><span class="p">;</span>
    <span class="k">font-size</span><span class="p">:</span> <span class="mi">90</span><span class="kt">%</span><span class="p">;</span>
    <span class="k">color</span><span class="p">:</span><span class="mh">#2e2e2e</span><span class="p">;</span>
    <span class="k">padding</span><span class="p">:</span> <span class="mi">4</span><span class="kt">px</span> <span class="mi">4</span><span class="kt">px</span><span class="p">;</span>
    <span class="k">margin</span><span class="p">:</span> <span class="mi">0</span> <span class="mi">8</span><span class="kt">px</span><span class="p">;</span>
    <span class="k">box-shadow</span><span class="p">:</span> <span class="mi">0</span><span class="kt">px</span> <span class="mi">1</span><span class="kt">px</span> <span class="mi">2</span><span class="kt">px</span> <span class="mi">0</span><span class="kt">px</span> <span class="nb">rgba</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mf">0.2</span><span class="p">);</span>
    <span class="k">border-radius</span><span class="p">:</span> <span class="mi">4</span><span class="kt">px</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>


<p>这样就相当于我把 JupyterLab Light 这个 Theme 稍微 Tweak 了一下。</p>
<p>另，我写的内容里，为了重点突出，特别定制了 <code>strong</code> 和 <code>em</code> 两个元素的显示，让它们以不同的颜色展示；又因为中文并不适合斜体展示，所以，把 <code>em</code> 的 <code>font-style</code> 设定为 <code>normal</code>……</p>
<h2>安装插件</h2>
<p>Jupyter notebook 经过很多年的发展，现在有很多扩展插件，但也有其中一些并不兼容最新的 Jupyter lab。不过，刚开始的时候用不着那么多插件，你只用其中的两个就足够开始了：</p>
<blockquote>
<ul>
<li><a href="https://github.com/jupyterlab/jupyterlab-toc">@jupyterlab/toc</a></li>
<li><a href="https://github.com/ryantam626/jupyterlab_sublime">ryantam626/jupyterlab_sublime</a></li>
</ul>
</blockquote>
<p>首先在用快捷键 <code>⌘ ,</code> 打开 jupyter lab 的 Advanced Settings，在 Extension Manager 中，添加 User Overrides：</p>
<div class="highlight"><pre><span></span><span class="p">{</span>
    <span class="nt">&quot;enabled&quot;</span><span class="p">:</span> <span class="kc">true</span>
<span class="p">}</span>
</pre></div>


<p>而后在 Terminal 执行以下命令安装插件：</p>
<div class="highlight"><pre><span></span>jupyter labextension install @jupyterlab/toc
jupyter labextension install @ryantam626/jupyterlab_sublime
jupyter lab build
</pre></div>


<p>toc 插件，自动将 ipynb 文件中的标题转换成目录。</p>
<p><img alt="123" src="https://github.com/jupyterlab/jupyterlab-toc/raw/master/toc.gif"></p>
<p>jupyterlab_sublime 则可以让你在 Jupyter lab 的 cell 中，使用跟 SublimeText 一样的快捷键，比如 <code>⌘ D</code> 能够多选其它与当前选中内容一样的内容；比如 <code>⌘</code> 加鼠标点击，可以生成多个可编辑点……</p>
<p><img alt="123" src="http://blog.rtwilson.com/wp-content/uploads/2016/03/IPyNbSublime.gif"></p>
<h2>常用快捷键</h2>
<p>以下是 MacOS 下 Jupyter lab 最常用的快捷键。快捷键在两种模式下执行，进入编辑模式用 <code>⏎</code>，回到命令模式用 <code>⎋</code>（ESC）。</p>
<p>另外，代码编辑过程中需要安装 Jupyterlab 插件 <a href="https://github.com/ryantam626/jupyterlab_sublime">@ryantam626/jupyterlab_sublime</a> 之后才能使用 “多行同时编辑功能”。</p>
<table>
<thead>
<tr>
<th>快捷键</th>
<th>说明</th>
<th>模式</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>ESC</code></td>
<td>从编辑模式回到命令模式</td>
<td>命令</td>
</tr>
<tr>
<td><code>A</code></td>
<td>在当前 Cell 之前插入一个 Cell</td>
<td></td>
</tr>
<tr>
<td><code>B</code></td>
<td>在当前 Cell 之后插入一个 Cell</td>
<td></td>
</tr>
<tr>
<td><code>D</code>, <code>D</code></td>
<td>连续按两次 <code>d</code> 键，删除当前 Cell</td>
<td></td>
</tr>
<tr>
<td><code>Y</code></td>
<td>将当前 Cell 设置为 Code Cell</td>
<td></td>
</tr>
<tr>
<td><code>M</code></td>
<td>将当前 Cell 设置为 Markdown Cell</td>
<td></td>
</tr>
<tr>
<td><code>^ ⇧ -</code></td>
<td>将当前 Cell 拆分为两个</td>
<td>编辑</td>
</tr>
<tr>
<td><code>⇧ M</code></td>
<td>合并选中的 Cells</td>
<td></td>
</tr>
<tr>
<td><code>⇧ J</code> or <code>⇧ ↓</code></td>
<td>连续向下选中 Cells</td>
<td></td>
</tr>
<tr>
<td><code>⇧ K</code> or <code>⇧ ↑</code></td>
<td>连续向上选中 Cells</td>
<td></td>
</tr>
<tr>
<td><code>⇧ ⏎</code> or <code>^ ⏎</code></td>
<td>运行当前 Cell 中的代码</td>
<td></td>
</tr>
<tr>
<td><code>⇧ L</code></td>
<td>显示/隐藏代码行号</td>
<td></td>
</tr>
<tr>
<td><code>⏎</code></td>
<td>当前 Cell 进入编辑模式</td>
<td>编辑</td>
</tr>
<tr>
<td><code>⇥</code></td>
<td>自动补全代码</td>
<td></td>
</tr>
<tr>
<td><code>⇧ ⇥</code></td>
<td>呼出当前光标下词汇的 Docstring</td>
<td></td>
</tr>
<tr>
<td><code>⌘ D</code></td>
<td>Sublime Keymap: 选中下一个相同字符串</td>
<td></td>
</tr>
<tr>
<td><code>⇧ ⌘ L</code></td>
<td>Sublime Keymap: 在选中的行内启动多行同时编辑</td>
<td></td>
</tr>
<tr>
<td><code>⌘ + Mouse Click</code></td>
<td>生成下一个可同时编辑的光标点</td>
<td></td>
</tr>
</tbody>
</table>
<h2>增加一些必要的快捷键</h2>
<p>在 Settings &gt; Keyboard Shortcuts 中，可以设定一些常用但系统并未给出的快捷键：</p>
<div class="highlight"><pre><span></span><span class="p">{</span>
    <span class="nt">&quot;notebook:move-cells-down-down&quot;</span><span class="p">:</span> <span class="p">{</span>
        <span class="nt">&quot;command&quot;</span><span class="p">:</span> <span class="s2">&quot;notebook:move-cell-down&quot;</span><span class="p">,</span>
        <span class="nt">&quot;keys&quot;</span><span class="p">:</span> <span class="p">[</span>
        <span class="s2">&quot;Alt J&quot;</span>
        <span class="p">],</span>
        <span class="nt">&quot;selector&quot;</span><span class="p">:</span> <span class="s2">&quot;.jp-Notebook:focus&quot;</span><span class="p">,</span>
        <span class="nt">&quot;title&quot;</span><span class="p">:</span> <span class="s2">&quot;Move Cells Down&quot;</span><span class="p">,</span>
        <span class="nt">&quot;category&quot;</span><span class="p">:</span> <span class="s2">&quot;Notebook Cell Operations&quot;</span>
    <span class="p">},</span>
    <span class="nt">&quot;notebook:move-cells-down-up&quot;</span><span class="p">:</span> <span class="p">{</span>
        <span class="nt">&quot;command&quot;</span><span class="p">:</span> <span class="s2">&quot;notebook:move-cell-up&quot;</span><span class="p">,</span>
        <span class="nt">&quot;keys&quot;</span><span class="p">:</span> <span class="p">[</span>
        <span class="s2">&quot;Alt K&quot;</span>
        <span class="p">],</span>
        <span class="nt">&quot;selector&quot;</span><span class="p">:</span> <span class="s2">&quot;.jp-Notebook:focus&quot;</span><span class="p">,</span>
        <span class="nt">&quot;title&quot;</span><span class="p">:</span> <span class="s2">&quot;Move Cells Down&quot;</span><span class="p">,</span>
        <span class="nt">&quot;category&quot;</span><span class="p">:</span> <span class="s2">&quot;Notebook Cell Operations&quot;</span>
    <span class="p">},</span>
    <span class="nt">&quot;notebook:enable-output-scrolling&quot;</span><span class="p">:</span> <span class="p">{</span>
        <span class="nt">&quot;command&quot;</span><span class="p">:</span> <span class="s2">&quot;notebook:enable-output-scrolling&quot;</span><span class="p">,</span>
        <span class="nt">&quot;keys&quot;</span><span class="p">:</span> <span class="p">[</span>
        <span class="s2">&quot;S&quot;</span>
        <span class="p">],</span>
        <span class="nt">&quot;selector&quot;</span><span class="p">:</span> <span class="s2">&quot;.jp-Notebook:focus&quot;</span><span class="p">,</span>
        <span class="nt">&quot;title&quot;</span><span class="p">:</span> <span class="s2">&quot;Enable output scrolling&quot;</span><span class="p">,</span>
        <span class="nt">&quot;category&quot;</span><span class="p">:</span> <span class="s2">&quot;Notebook Cell Operations&quot;</span>
    <span class="p">},</span>
    <span class="nt">&quot;notebook:disable-output-scrolling&quot;</span><span class="p">:</span> <span class="p">{</span>
        <span class="nt">&quot;command&quot;</span><span class="p">:</span> <span class="s2">&quot;notebook:disable-output-scrolling&quot;</span><span class="p">,</span>
        <span class="nt">&quot;keys&quot;</span><span class="p">:</span> <span class="p">[</span>
        <span class="s2">&quot;Alt S&quot;</span>
        <span class="p">],</span>
        <span class="nt">&quot;selector&quot;</span><span class="p">:</span> <span class="s2">&quot;.jp-Notebook:focus&quot;</span><span class="p">,</span>
        <span class="nt">&quot;title&quot;</span><span class="p">:</span> <span class="s2">&quot;Enable output scrolling&quot;</span><span class="p">,</span>
        <span class="nt">&quot;category&quot;</span><span class="p">:</span> <span class="s2">&quot;Notebook Cell Operations&quot;</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>


<p>这样就添加了 4 个快捷键：</p>
<blockquote>
<ul>
<li><code>⌥ J</code>: Move selected cells down</li>
<li><code>⌥ K</code>: Move selected cells up</li>
<li><code>S</code>: Enable output scrolling</li>
<li><code>⌥ S</code>: Disable output scrolling</li>
</ul>
</blockquote>
<p>比如 Move Selected cells up：</p>
<p><img alt="123" src="https://user-images.githubusercontent.com/86304/37438938-b5bc4994-27b2-11e8-8f58-184a58b33ba4.gif"></p>
<h2>输出所有变量内容</h2>
<p>默认情况下，Code Cell 只输出最后一个可以被 evaluate 的值，用 <code>_</code> 代表之前刚刚被 evaluate 的值。</p>
<div class="highlight"><pre><span></span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span>
</pre></div>


<div class="highlight"><pre><span></span>[1, 2, 3]
</pre></div>


<div class="highlight"><pre><span></span><span class="n">_</span> <span class="c1"># 执行完上面的 Cell，试试这个 Cell; 而后执行完下面的 Cell 之后再重新执行一次当前这个 Cell</span>
</pre></div>


<div class="highlight"><pre><span></span>[1, 2, 3]
</pre></div>


<div class="highlight"><pre><span></span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">}</span>
</pre></div>


<div class="highlight"><pre><span></span>{1, 2, 3}
</pre></div>


<p>于是，为了显示最近 evaluate 的多个值，我们总是不得不使用很多的 <code>print()</code>……</p>
<p>如果觉得这事比较烦的话，可以在 Cell 最上面写上：</p>
<div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">IPython.core.interactiveshell</span> <span class="kn">import</span> <span class="n">InteractiveShell</span>
<span class="n">InteractiveShell</span><span class="o">.</span><span class="n">ast_node_interactivity</span> <span class="o">=</span> <span class="s2">&quot;all&quot;</span>
</pre></div>


<p>如果还想更省事一点，就把这个设置写入配置文件：</p>
<div class="highlight"><pre><span></span><span class="n">c</span><span class="o">.</span><span class="n">InteractiveShell</span><span class="o">.</span><span class="n">ast_node_interactivity</span> <span class="o">=</span> <span class="s2">&quot;all&quot;</span>
</pre></div>


<div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">IPython.core.interactiveshell</span> <span class="kn">import</span> <span class="n">InteractiveShell</span>
<span class="n">InteractiveShell</span><span class="o">.</span><span class="n">ast_node_interactivity</span> <span class="o">=</span> <span class="s2">&quot;all&quot;</span>

<span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">}</span>
</pre></div>


<div class="highlight"><pre><span></span>(1, 2, 3)

{1, 2, 3}
</pre></div>


<h2>魔法函数</h2>
<p>在 Code Cell 里，可以运行一些 “魔法函数”（Magic Functions），这是秉承了 IPython 的特性。绝大多数在 IPython 里能够使用的魔法函数在 Jupyterlab 里都可以直接使用。完整的 IPython 魔法函数请参照：</p>
<blockquote>
<p>https://ipython.readthedocs.io/en/stable/interactive/magics.html</p>
</blockquote>
<p>Jupyterlab 里较为常用的魔法函数整理如下：</p>
<table>
<thead>
<tr>
<th>魔法函数</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>%lsmagic</code></td>
<td>列出所有可被使用的 Jupyter lab 魔法函数</td>
</tr>
<tr>
<td><code>%run</code></td>
<td>在 Cell 中运行 <code>.py</code> 文件：<code>%run file_name</code></td>
</tr>
<tr>
<td><code>%who</code></td>
<td>列出所有当前 Global Scope 中的变量；类似的还有：<code>%who df</code>，<code>%whos</code></td>
</tr>
<tr>
<td><code>%env</code></td>
<td>列出当前的环境变量</td>
</tr>
<tr>
<td><code>%load</code></td>
<td>将其他文件内容导入 Cell，<code>%load source</code>，<code>source</code> 可以是文件名，也可以是 URL。</td>
</tr>
<tr>
<td><code>%time</code></td>
<td>返回 Cell 内代码执行的时间，相关的还有 <code>%timeit</code></td>
</tr>
<tr>
<td><code>%writefile</code></td>
<td>把 Cell 的内容写入文件，<code>%write file_name</code>；%write -a file_name，<code>-a</code> 是追加</td>
</tr>
<tr>
<td><code>%matplotlib inline</code></td>
<td>行内展示 matplotlib 的结果</td>
</tr>
<tr>
<td><code>%%bash</code></td>
<td>运行随后的 shell 命令，比如 %%bash ls；与之类似的还有 <code>%%HTML</code>，<code>%%python2</code>，<code>%%python3</code>，<code>%%ruby</code>，<code>%%perl</code>……</td>
</tr>
</tbody>
</table>
<h2>桌面版 Jupyter App</h2>
<h3>Nteract</h3>
<p>支持各个操作系统，很好看、很好用。有一个小缺点是，不支持 <code>input()</code> 函数的调用。</p>
<blockquote>
<p>https://nteract.io/desktop</p>
</blockquote>
<p><img alt="https://cloud.githubusercontent.com/assets/836375/18421299/d95ad398-783b-11e6-8b23-d54cf7caad1e.png" src="https://cloud.githubusercontent.com/assets/836375/18421299/d95ad398-783b-11e6-8b23-d54cf7caad1e.png"></p>
<h3>Pineapple</h3>
<p>只支持 MacOS，也很好用 —— 缺点就是很难看……</p>
<blockquote>
<p>https://nwhitehead.github.io/pineapple/</p>
</blockquote>
<p><img alt="https://nwhitehead.github.io/pineapple/images/sshots.png" src="https://nwhitehead.github.io/pineapple/images/sshots.png?raw=true"></p>
    </div><!-- /.entry-content -->

  </article>
</section>
        <section id="extras" class="body">
                <div class="blogroll">
                        <h2>blogroll</h2>
                        <ul>
                            <li><a href="http://getpelican.com/">Pelican</a></li>
                            <li><a href="http://python.org/">Python.org</a></li>
                            <li><a href="http://jinja.pocoo.org/">Jinja2</a></li>
                            <li><a href="#">You can modify those links in your config file</a></li>
                        </ul>
                </div><!-- /.blogroll -->
                <div class="social">
                        <h2>social</h2>
                        <ul>

                            <li><a href="#">You can add links in your config file</a></li>
                            <li><a href="#">Another social link</a></li>
                        </ul>
                </div><!-- /.social -->
        </section><!-- /#extras -->

        <footer id="contentinfo" class="body">
                <p>Powered by <a href="http://www.maxiaoan.com/">Maxiaoan</a></p>
        </footer><!-- /#contentinfo -->

</body>
</html>